var smcInstance;
var displayMode:Object;
var dynamicPortletArr:Object;

var friendListResult:Object;
var activityListResult:Object;

var pageNum:Number;
var pageTotal:Number;

function init(param, data, slot)
{
	Packages.model.Context.activeContent=this;
	
	// Param.init
	smcInstance=mlf.mcmanager.SMCManager.getInstance(slot);
	displayMode=new Object();
	displayMode["activityListPanel"]="normal";
	displayMode["friendListPanel"]="normal";
	displayMode["friendInvitePanel"]="normal";

	dynamicPortletArr=new Object();
	
	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);
	var expandPortletArr:Array=new Array();
	expandPortletArr.push(basic["activityListPanel"]);
	expandPortletArr.push(basic["friendListPanel"]);
	expandPortletArr.push(basic["friendInvitePanel"]);
	var styleObj:Object=new Object();
	styleObj.mainLocation=new Object();
	styleObj.subLocation=new Object();
	styleObj.mainLocation.x=0;
	styleObj.mainLocation.y=40;
	styleObj.mainLocation.width=525;
	styleObj.mainLocation.height=450;
	styleObj.subLocation.bx=0;
	styleObj.subLocation.by=0;
	styleObj.subLocation.dx=272;
	styleObj.subLocation.dy=0;
	styleObj.subLocation.width=252;
	styleObj.subLocation.height=40;
	Packages.system.display.JAccordion.setExpandPortletArr(expandPortletArr, styleObj);
	
	// Render.init
	Packages.system.display.SceneTransition.rollOut();

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "back_print"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onJAccodionCellEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onJAccodionCellEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);
	
	pageNum=1;

	loadFriendList();
	// loadActivityList();
}

function onJAccodionCellEvent(evt:Object)
{
	// Packages.log(evt.type+": "+evt.param);

	if (evt.data[evt.param]!="expand" && displayMode[evt.param]!="expand") return;
	
	if (evt.param=="activityListPanel") 
	{
		displayMode.activityListPanel=evt.data.activityListPanel;
		if (dynamicPortletArr.activityListPanel=="activityList")
		{
			loadActivityList();
		}
	}
	else if (evt.param=="friendListPanel") 
	{
		displayMode.friendListPanel=evt.data.friendListPanel;
		
		if (dynamicPortletArr.friendListPanel=="friendList")
		{
			loadFriendList();
		}
	}
}

function loadFriendList(param, data)
{
	dynamicPortletArr.friendListPanel="friendList";
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "friendList"));
	if (displayMode.friendListPanel=="normal")
	{
		Packages.modula.RequestCommand.request("HTTP", "restlet/members/"+Packages.model.UserProfile.data.memberId+"/friends?startIndex=0&count=16", null, Packages.model.vo.UserListView, displayMode.friendListPanel, this, {container:"friendList", slot:this._name, uri:"app/content/Friends.xml", dynamicPortlet:dynamicPortletArr.friendListPanel, saved:"friendListResult", initMethod:"lib:notes.components.content.Friends.onFriendListInit"});
	}
	else 
	{
		//
		if (data.pageNum==null)
		{
			pageNum=1;
		}
		else pageNum=data.pageNum;

		startIndex=(pageNum-1)*15;

		Packages.modula.RequestCommand.request("HTTP", "restlet/members/"+Packages.model.UserProfile.data.memberId+"/friends?startIndex="+startIndex+"&count=30", null, Packages.model.vo.UserListView, displayMode.friendListPanel, this, {container:"friendList", slot:this._name, uri:"app/content/Friends.xml", dynamicPortlet:dynamicPortletArr.friendListPanel, saved:"friendListResult", initMethod:"lib:notes.components.content.Friends.onFriendListInit", enterEvent:"loadFriendList"});
	}
}

function onFriendListInit()
{
	var containermc=Packages.system.mlf.GateKeeper.scanElement(this._name, dynamicPortletArr.friendListPanel);
	adjustLayout(displayMode.friendListPanel, containermc);
	onCoverInit(displayMode.friendListPanel, "friendList");
}

function adjustLayout(mode:String, containermc:MovieClip)
{
	var height:Number;
	var width:Number;
	var evtTargetArr:Array=new Array();

	var rst=this[containermc._name+"Result"];

	if (mode=="normal")
	{
		height=160;
		width=500;
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "moreSpot_friendList"));
	}
	else if (mode=="expand")
	{
		height=450;
		width=500;
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListPreSpot"));
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListNextSpot"));
		
		var userListTotalPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListTotalPage");
		pageTotal=Math.floor(rst.count/15)+1;
		userListTotalPage.text=pageTotal;

		var userListCurrentPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListCurrentPage");
		userListCurrentPage.text=pageNum;
		var tf_fmt:TextFormat=new TextFormat();
		tf_fmt.align="right";
		userListCurrentPage.setTextFormat(tf_fmt);
	}

	var param:String="area";
	param=param+":"+height+":"+width+":noSlider";
	smcInstance.updateStyle("layout", containermc, param);
	smcInstance.applyStyle();

	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	Packages.system.render.Transition.fadeType1(containermc);
}

function onCoverInit(mode:String, container:String)
{
	var processId;
	var coverContainer;
	var minWidth:Number;
	var minHeight:Number;

	var rst=this[container+"Result"];

	if (mode=="normal")
	{
		minHeight=50;
		minWidth=50;
	}
	else if (mode=="expand")
	{
		minHeight=80;
		minWidth=80;
	}

	for (var i=0; i<rst.members.length; i++)
	{
		processId=rst.members[i].memberId;
		coverContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "picCover_"+processId);
		var listener:Object=new Object();
		listener.onLoadInit=function(target_mc)
		{
			var zoom=Packages.system.utils.ImageUtil.minSize(target_mc, minWidth, minHeight);
			target_mc._xscale=zoom;
			target_mc._yscale=zoom;
			// centralize the target
			var offsetX:Number=(target_mc._width-minWidth)/2;
			target_mc._x=-offsetX;
			var offsetY:Number=(target_mc._height-minHeight)/2;
			target_mc._y=-offsetY;
		}
		if (rst.members[i].image!=null)
		{
			// @tmp - image url
			rst.members[i].image="app/img/paint.jpg";
			mlf.mcmanager.dynamicloading.LoadApp.loadAppLoader(rst.members[i].image, coverContainer, "img", listener); 
		}
	}
}

function expandMore(container:String)
{
	var cellPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, container+"Panel");
		
	Packages.system.display.JAccordion.invokeReleaseEvent(mlf.mcmanager.PathResolver.drawPath(cellPortlet.header));
}

function previousMemberList()
{
	if (pageNum>1)
	{
		pageNum--;
		loadFriendList(null, {pageNum:pageNum});
	}
}

function nextMemberList()
{
	if (pageNum<pageTotal)
	{
		pageNum++;
		loadFriendList(null, {pageNum:pageNum});
	}
}

function onReleaseButtonEvent(evt:Object)
{
	if (evt.param=="memberListPreSpot")
	{
		previousMemberList();
	}
	else if (evt.param=="memberListNextSpot")
	{
		nextMemberList();
	}
	else if (evt.param.indexOf("memberItem")>-1)
	{
		var selectedMemberId=evt.param.substr(11);
		Packages.system.utils.ComponentsUtil.popupSelectedDataDetail(friendListResult, selectedMemberId, "MemberDetail", "content/myReadjoy/PersonPage");
	}
	else if (evt.param.indexOf("moreSpot")>-1)
	{
		var container=evt.param.substr(9);
		expandMore(container);
	}
}

function handleResponseResult(rsp:Object)
{
	var dynamicPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.dynamicPortlet);
	if (dynamicPortlet!=null)
	{
		for (var a in dynamicPortlet)
		{
			if (typeof(dynamicPortlet[a])=="movieclip")
			{
				dynamicPortlet[a].removeMovieClip();
			}
		}
		Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	}
	else Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);

	if (rsp.component.data.saved!=null)
	{
		this[rsp.component.data.saved]=rsp.service.result;
	}

	Packages.system.display.Progress.removeInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.container));
}